﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.HYCRM.BLL;
using Koonsoft.HYCRM.Model;

public partial class Deliver_DeliverHandle : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    /// <summary>
    /// 显示订单数据
    /// </summary>
    private void BindData()
    {
        if (Request.QueryString["key"] != null)
        {
            var orderid = Request.QueryString["key"];

            if (!string.IsNullOrEmpty(orderid))
            {
                if (orderid.Length == 10)
                {
                    Label1.Text = orderid;
                    OrderID.Value = orderid;

                    var order = new OrderBLL().GetOrderInfoByOrderID(orderid);
                    var company = new CompanyBLL().GetCompanyInfoByOrderID(orderid);

                    OrderTime.Text = order.OrderTime.ToString("yyyy-MM-dd");
                    BranchName.Text = new BranchBLL().GetBranchNameByBranchID(order.BranchID);
                    CompanyChineseName.Text = order.ChineseCompanyName;
                    CompanyEnglishName.Text = order.EnglishCompanyName;

                    if (company != null)
                    {
                        CompanyID.Text = company.CompanyID;
                        RegDate.Text = company.RegisterDate.ToString("yyyy-MM-dd");
                    }

                    if (string.IsNullOrEmpty(company.CompanyID) || company.CompanyID == "待录入" || company.CompanyID == "属于分行" || company.CompanyID == "分行")
                    {
                        trReSign.Visible = true;
                    }
                    else
                    {
                        trReSign.Visible = false;
                    }
                }
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["key"] != null)
        {
            var orderid = Request.QueryString["key"];

            if (!string.IsNullOrEmpty(orderid))
            {
                if (orderid.Length == 10)
                {
                    var order = new OrderBLL().GetOrderInfoByOrderID(orderid);
                    var company = new CompanyBLL().GetCompanyInfoByOrderID(orderid);

                    if (string.IsNullOrEmpty(company.CompanyID) || company.CompanyID == "待录入" || company.CompanyID == "属于分行" || company.CompanyID == "分行")
                    {
                        if (!string.IsNullOrEmpty(ReSignCompanyID.Text.Trim()))
                        {
                            new CompanyBLL().FileHandleByCompanySerialNumber(ReSignCompanyID.Text.Trim(), company.EnglishName, company.ChineseName, company.SerialNumber, RegDate.Text);
                        }
                    }
                    else
                    {
                        new CompanyBLL().UpdateRegisterDate(RegDate.Text,company.SerialNumber);
                    }

                }
            }

            // 写入订单轨迹
            var empolyeeID = new EmpolyeeBLL().GetEmpolyeeInfoByLoginName(User.Identity.Name).EmpolyeeID;
            new OrderTrackBLL().AddTrack(OrderID.Value, empolyeeID, Koonsoft.HYCRM.Model.OrderStatus.CenterDeliver, 1, Remark.Text.Trim());

            // 通知订单状态改变
            new OrderBLL().ChangeOrderStatusByOrderID(OrderID.Value, Koonsoft.HYCRM.Model.OrderStatus.FinanceAudit);

            Response.Redirect("OrdersForBranches.aspx");
        }
    }
}